<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 小夏 < 449134904@qq.com>
// +----------------------------------------------------------------------
namespace app\admin\controller;

use app\classes\model\ClassModel;
use cmf\controller\AdminBaseController;
use think\Db;
use app\admin\model\AdminMenuModel;

class IndexController extends AdminBaseController
{

    public function _initialize()
    {
        $adminSettings = cmf_get_option('admin_settings');
        if (empty($adminSettings['admin_password']) || $this->request->path() == $adminSettings['admin_password']) {
            $adminId = cmf_get_current_admin_id();
            if (empty($adminId)) {
                session("__LOGIN_BY_CMF_ADMIN_PW__", 1);//设置后台登录加密码
            }
        }

        parent::_initialize();
    }

    /**
     * 后台首页
     */
    public function index()
    {
        $uid = cmf_get_current_admin_id();

        $adminMenuModel = new AdminMenuModel();
        $menus = $adminMenuModel->menuTree();
        $where = [
            'm.type' => 0,  //站内信
            'mu.msg_status' => 0, //未读
            'mu.receive_uid' => $uid,
            'm.shop_id' => $this->admin_shop_id,
        ];

        //获取未读消息个数
        $total = Db::name('message_user')->alias('mu')
            ->join('message m', 'm.id=mu.msg_id')
            ->join('user u', 'm.send_uid=u.id')
            ->where($where)
            ->count();
        if ($total > 0) {
            //获取未读消息前几条
            $info = Db::name('message_user')->alias('mu')
                ->field('mu.*,m.shop_id,m.send_uid,m.subject,m.content,m.type,m.create_time,u.realname as user_name')
                ->join('message m', 'm.id=mu.msg_id')
                ->join('user u', 'm.send_uid=u.id')
                ->where($where)
                ->limit(5)
                ->order('mu.id', 'desc')
                ->select()
                ->toArray();
        } else {
            $info = [];
            $total = 0;
        }

        //(steven)获取我的消息
        $msgMenuData = [
            'shop_id' => $this->admin_shop_id,
            'controller' => 'Message',
            'action' => 'info',
        ];
        $msgMenu = $adminMenuModel->where($msgMenuData)->find();

        //(steven)获取我的消息详情
        $msgInfoData = [
            'shop_id' => $this->admin_shop_id,
            'controller' => 'Message',
            'action' => 'infoDetail',
        ];
        $msgInfoMenu = $adminMenuModel->where($msgInfoData)->find();

        $admin = Db::name("user")->where(['id' => $uid])->find();
        $this->assign('admin', $admin);
        $this->assign("menus", $menus);
        $this->assign("msgMenu", $msgMenu);
        $this->assign("msgInfoMenu", $msgInfoMenu);
        $this->assign("total", $total);
        $this->assign("myInfo", $info);

        return $this->fetch();
    }

    /**
     * 移动端后台首页
     */
    public function mobileindex()
    {

        return $this->fetch();
    }
}
